/**
 * Copyright (c) 2009-2012 XTEAM
 * All rights reserved.
 */
/**
 * File：AdvCfgDaoImpl.java
 * History:
 *         2012-6-28: Initially created, Chrise.
 */
package com.xteam.ecsystem.dao.Impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.xteam.ecsystem.dao.BaseDao;
import com.xteam.ecsystem.dao.IAdvCfgDao;
import com.xteam.ecsystem.entities.SysAdvConfig;
import com.xteam.ecsystem.utils.PageCenterStation;

/**
 * 广告配置数据访问实现。
 * @author Chrise
 */
@Repository
public class AdvCfgDaoImpl implements IAdvCfgDao {
	
	@Autowired
	private BaseDao<SysAdvConfig> baseDao;
	
	/**
	 * @see com.xteam.ecsystem.dao.IAdvCfgDao#getConfigs(int, java.lang.String, int, int, int, int, int)
	 */
	public PageCenterStation<SysAdvConfig> getConfigs(int type, String name, 
		int page, int position, int state, int first, int max) {
		String cdt = "";
		if (type != INVALID_TYPE) cdt = " where sacType = " + type;
		if (name != INVALID_NAME) {
			if ("".equals(cdt)) {
				cdt = " where sacName like '%" + name + "%'";
			} else {
				cdt += " and sacName like '%" + name + "%'";
			}
		}
		if (page != INVALID_PAGE) {
			if ("".equals(cdt)) {
				cdt = " where sacPage = " + page;
			} else {
				cdt += " and sacPage = " + page;
			}
		}
		if (position != INVALID_POSITION) {
			if ("".equals(cdt)) {
				cdt = " where sacPosition = " + position;
			} else {
				cdt += " and sacPosition = " + position;
			}
		}
		if (state != INVALID_STATE) {
			if ("".equals(cdt)) {
				cdt = " where sacState = " + state;
			} else {
				cdt += " and sacState = " + state;
			}
		}
		
		String countHql = "select count(sacId) from SysAdvConfig" + cdt;
		String queryHql = "from SysAdvConfig" + cdt;
		
		return this.baseDao.queryPage(countHql, queryHql, first, max);
	}
	
	/**
	 * @see com.xteam.ecsystem.dao.IAdvCfgDao#updateState(java.lang.String, int)
	 */
	public void updateState(String id, int state) {
		String hql = "update SysAdvConfig set sacState = " + state + " where sacId = '" + id + "'";
		this.baseDao.executeUpdate(hql);
	}
	
	/**
	 * @see com.xteam.ecsystem.dao.IAdvCfgDao#deleteConfigs(java.util.List)
	 */
	public void deleteConfigs(List<String> ids) {
		String cdt = "'" + ids.get(0) + "'";
		for (int index = 1; index < ids.size(); index ++) {
			cdt += ", '" + ids.get(index) + "'";
		}
		
		String hql = "delete from SysAdvConfig where sacId in (" + cdt + ")";
		this.baseDao.executeUpdate(hql);
	}
	
	/**
	 * @see com.xteam.ecsystem.dao.IAdvCfgDao#isExist(java.lang.String)
	 */
	public boolean isExist(String name) {
		String hql = "select count(sacId) from SysAdvConfig where sacName = '" + name + "'";
		if (this.baseDao.count(hql) > 0) {
			return true;
		}
		
		return false;
	}
	
	/**
	 * @see com.xteam.ecsystem.dao.IAdvCfgDao#isExist(java.lang.String, java.lang.String)
	 */
	public boolean isExist(String id, String name) {
		String hql = "select count(sacId) from SysAdvConfig where sacId <> '" + id + 
			"' and sacName = '" + name + "'";
		if (this.baseDao.count(hql) > 0) {
			return true;
		}
		
		return false;
	}
	
	/**
	 * @see com.xteam.ecsystem.dao.IAdvCfgDao#insertConfig(com.xteam.ecsystem.entities.SysAdvConfig)
	 */
	public void insertConfig(SysAdvConfig sac) {
		this.baseDao.save(sac);
	}
	
	/**
	 * @see com.xteam.ecsystem.dao.IAdvCfgDao#updateConfig(com.xteam.ecsystem.entities.SysAdvConfig)
	 */
	public void updateConfig(SysAdvConfig sac) {
		this.baseDao.update(sac);
	}
	
	/**
	 * @see com.xteam.ecsystem.dao.IAdvCfgDao#getConfig(java.lang.String)
	 */
	public SysAdvConfig getConfig(String id) {
		String hql = "from SysAdvConfig where sacId = '" + id + "'";
		return this.baseDao.findUniqueObject(hql);
	}
	
	/**
	 * @see com.xteam.ecsystem.dao.IAdvCfgDao#getAdvertises(int, int, int, int)
	 */
	public List<SysAdvConfig> getAdvertises(int common, int page, int position, int state) {
		String cdt = " and sacPage in (" + common;
		if (page != INVALID_PAGE) {
			cdt += ", " + page;
		}
		cdt += ")";
		if (position != INVALID_POSITION) {
			cdt += " and sacPosition = " + position;
		}
		String hql = "from SysAdvConfig where sacState = " + state + cdt + " order by sacOrder desc";
		
		return this.baseDao.queryList(hql);
	}
}
